package cn.itcast.bos.web.action.base;

import java.io.IOException;
import java.util.Collection;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import cn.itcast.bos.constant.Constants;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONArray;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;

@Controller
@Namespace("/")
@ParentPackage("struts-default")
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea> {

	private Integer courierId;
	private Integer takeTimeId;

	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}
	
	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	private String[] customerIds;

	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	@Autowired
	private FixedAreaService fixedAreaService;

	// 保存
	@Action(value = "fixedArea_save", results = {
			@Result(type = "redirect", name = "success", location = "./pages/base/fixed_area.html") })
	public String save() {
		fixedAreaService.save(model);
		return SUCCESS;
	}

	// 分页显示
	@Action(value = "fixedArea_showPage")
	public void findAll() {
		Pageable pageable = new PageRequest(page, rows);
		Specification<FixedArea> specification = new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				return null;
			}
		};
		Page<FixedArea> pageData = fixedAreaService.findAll(specification, pageable);
		writeJson(pageData);
	}

	// 查找未关联
	@Action(value = "fixedArea_noAssociation")
	public void noAssociation() {
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		Collection<? extends Customer> collection = WebClient
				.create(Constants.CRM_MANAGEMENT_URL+"/services/customerService/association").accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		String json = JSONArray.toJSONString(collection);
		try {
			ServletActionContext.getResponse().getWriter().write(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 查找已关联
	@Action(value = "fixedArea_Association")
	public void association() {
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		Collection<? extends Customer> collection = WebClient
				.create(Constants.CRM_MANAGEMENT_URL+"/services/customerService/association/" + model.getId())
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		String json = JSONArray.toJSONString(collection);
		try {
			ServletActionContext.getResponse().getWriter().write(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 设置关联用户
	@Action(value = "fixedArea_updateAssociation", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String updateAssociation() {
		String customerIdStr = StringUtils.join(customerIds, ",");
		if (customerIdStr == null) {
			customerIdStr = "";
		}
		WebClient.create(Constants.CRM_MANAGEMENT_URL+"/services/customerService/updateAssociation")
				.query("customerFixedAreaId", model.getId()).query("customerIdStr", customerIdStr).put(null);
		return SUCCESS;
	}

	// 设置关联快递员
	@Action(value = "fixedArea_associationCourierToFixedArea", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String associationCourier() {
		fixedAreaService.assassociationCourier(model.getId(), courierId, takeTimeId);
		return SUCCESS;
	}
}
